<template>
  <el-card shadow="never" header="修改密码">
    <el-alert
      title="密码更新成功后，您将被重定向到登录页面，您可以使用新密码重新登录。"
      type="info"
      show-icon
      style="margin-bottom: 15px"
    />
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="120px"
      style="margin-top: 20px"
    >
      <el-form-item label="当前密码" prop="userPassword">
        <el-input
          v-model="form.userPassword"
          type="password"
          show-password
          placeholder="请输入当前密码"
        ></el-input>
        <div class="el-form-item-msg">必须提供当前登录用户密码才能进行更改</div>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input
          v-model="form.newPassword"
          type="password"
          show-password
          placeholder="请输入新密码"
        ></el-input>
        <sc-password-strength v-model="form.newPassword"></sc-password-strength>
        <div class="el-form-item-msg">请输入包含英文、数字的6位以上密码</div>
      </el-form-item>
      <el-form-item label="确认新密码" prop="confirmNewPassword">
        <el-input
          v-model="form.confirmNewPassword"
          type="password"
          show-password
          placeholder="请再次输入新密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存密码</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import scPasswordStrength from "@/components/scPasswordStrength";
import publicjs from "@/api/model/publicjs";
import tool from "@/utils/tool";
export default {
  components: {
    scPasswordStrength,
  },
  data() {
    return {
      form: {
        userPassword: "",
        newPassword: "",
        confirmNewPassword: "",
      },
      rules: {
        userPassword: [{ required: true, message: "请输入当前密码" }],
        newPassword: [
          { required: true, message: "请输入新密码" },
          { min: 6, message: "名称长度不能小于6位" },
          { max: 20, message: "名称长度不能超过20位" },
        ],
        confirmNewPassword: [
          { required: true, message: "请再次输入新密码" },
          {
            validator: (rule, value, callback) => {
              if (value !== this.form.newPassword) {
                callback(new Error("两次输入密码不一致"));
              } else {
                callback();
              }
            },
          },
        ],
      },
      saveState:false,
    };
  },
  methods: {
    async save() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          const data={
            CurrentPassword:this.form.userPassword,
            Password:this.form.newPassword
          };
          var returnValue = await this.$API.auth.PostProcessApi.post("account/current-passwrod",data);
          if (returnValue.status == 200) {
            this.$alert(
              "密码修改成功，是否跳转至登录页使用新密码登录",
              "修改成功",
              {
                type: "success",
                center: true,
              }
            )
              .then(() => {
                this.$router.replace({
                  path: "/login",
                });
              })
              .catch(() => {
                this.$message.success("操作失败");
              });
          } else this.$message.error(returnValue.msg);
        } else {
          return false;
        }
      });
    },
    async initPurview(orgCode = "") {
			orgCode = orgCode.trim(",");
			let loginUser = this.$TOOL.data.get("USER_INFO").userCode;
			if (orgCode == "" || orgCode.indexOf(",") > 0) {
				this.saveState = false;
			} else {
				var head = new publicjs.head();
				publicjs.initHead(head, "purviewbtn");
				var ed = new publicjs.entitydata(head);
				var eo = new publicjs.entityobj();
				eo.ValueList = {
					PurviewId: "userCenter",
					OrgCode: orgCode,
					UserCode: loginUser.userCode,
				};
				ed.Data.Content.ObjectList.Object.push(eo);
				try {
					var returnValueStr = await this.$API.auth.objProcess.post(
						ed
					);
					var returnValue = JSON.parse(returnValueStr);
					console.log(returnValue.content);
					// eslint-disable-next-line no-debugger
					//debugger;
					if (returnValue.Result.OpFlag == "1") {
						let btnList = returnValue.Result.Content;
						//all,add
						if (btnList == "all") {
							this.saveState = true;
						} else {
							this.saveState = btnList.indexOf("password") >= 0;
						}
						console.log(btnList);
					} else {
						this.$message.error(
							"操作失败:" + returnValue.Result.OpMsg
						);
					}
				} catch (error) {
					this.loading = false;
					this.$alert(error.message, "提示", { type: "error" });
					return false;
				}
			}
		},
  },
  async mounted() {
		let currentOrgCode = tool.data.get("USER_INFO").currentOrgCode;
		await this.initPurview(currentOrgCode);
	},
};
</script>

<style>
</style>
